<?php
define('BASEPATH', dirname(__FILE__));
include( dirname(__FILE__) . "/../../../application/config/webset.php");
$uploaddir = 'uploads/';
$thumbnaildir = 'thumbnail/';
include('SimpleImage.php');
$image = new SimpleImage();
$uploadfile = $uploaddir. $_FILES['file']['name'];
$upload_path = '/uploads/product/'.date("Ymd")."";
$dir = dirname(__FILE__) . "/../../..".$upload_path;
!is_dir($dir) && @mkdir($dir, 0755, true);

$upload_path_thumbnail = '/uploads/thumbnail/'.date("Ymd")."";
$dirthumb = dirname(__FILE__) . "/../../..".$upload_path_thumbnail;
!is_dir($dirthumb) && @mkdir($dirthumb, 0755, true);

$filetype = strtolower(strrchr($_FILES['file']['name'], "."));
$finame = "new_" . date("YmdHms_", time()) . rand(10, 99);
$tempnameds =$finame. $filetype;
$newfile = $dir."/$tempnameds";
$upfile = $_FILES['file']["tmp_name"];
$thumbnail = $dirthumb."/".$finame."_thumb" . $filetype;

$resizeToHeight = $config['resizeToHeight']? $config['resizeToHeight']:150;
$resizeToWidth = $config['resizeToWidth']? $config['resizeToWidth']:150;;

if (@move_uploaded_file($upfile, $newfile)) {
	$filepath =  $upload_path."/".$tempnameds;
	$filethumbpath =  $upload_path_thumbnail."/".$finame."_thumb" . $filetype;
	$src_img =$newfile;
	$dst_img =$thumbnail;
	$image->load($src_img);
	$image->resizeToWidth($resizeToHeight);
	$image->resizeToHeight($resizeToWidth);
	$image->save($dst_img);
	echo "<input type=\"hidden\" name=\"filepath[]\" id=\"filepath\" value=\"$filepath\">"; 
	echo "<input type=\"hidden\" name=\"filethumbpath[]\" id=\"filethumbpath\" value=\"$filethumbpath\">"; 
} else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print "<pre>";
	print_r($_FILES);
	print "</pre>";
}


?>